package com.example.drive.audit.service.impl;


import com.example.drive.admin.pojo.dto.UpdatePwdDTO;

import com.example.drive.admin.pojo.vo.UserLoginVO;

import com.example.drive.audit.mapper.AuditMapper;
import com.example.drive.audit.pojo.dto.AuditDTO;
import com.example.drive.audit.pojo.entity.Audit;
import com.example.drive.audit.service.AuditService;
import com.example.drive.common.ServiceCode;
import com.example.drive.common.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class AuditServiceImpl implements AuditService {
      @Autowired
      private AuditMapper auditMapper;

    /**
     * 更改密码
     * @param updatePwdDTO
     * @param userLoginVO
     */
    @Override
    public void change(UpdatePwdDTO updatePwdDTO, UserLoginVO userLoginVO){
        Audit audit = auditMapper.getByName(userLoginVO.getUsername());
        if(audit==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该用户！");
        }
        if(!updatePwdDTO.getOldPwd().equals(audit.getPassword())){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"密码不一致！");
        }
        audit.setPassword(updatePwdDTO.getNewPwd());
        int result = auditMapper.update(audit);
        if(result<=0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"修改失败!");
        }
    }

    /**
     * 更新个人信息
     * @param auditDTO
     * @param userLoginVO
     */
    @Override
    public void updateInfo(AuditDTO auditDTO,UserLoginVO userLoginVO){
        Audit audit = auditMapper.getByName(userLoginVO.getUsername());
        if(audit==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该用户！");
        }
        audit.setUsername(auditDTO.getUsername());
        audit.setAge(auditDTO.getAge());
        audit.setSex(auditDTO.getSex());
        audit.setTel(auditDTO.getTel());
        int result = auditMapper.update(audit);
        if(result<=0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"修改失败!");
        }
    }

    /**
     * 显示个人信息
     * @param id
     * @return
     */
    @Override
    public Audit search(Integer id){
        Audit audit = auditMapper.getById(id);
        if(audit==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该审核员！");
        }
        return audit;
    }



}
